# 菜品类别信息
from django.db.models import Q
from django.shortcuts import render
from django.http import JsonResponse
from datetime import datetime

from myadmin.models import Category, Shop, Product
from myadmin.utils import get_page, params_status


def index(request, pIndex=1):
    clist, mywhere, kw = params_status(Category, request)

    if kw:
        shop_id = None
        shop = Shop.objects.filter(name__contains=kw).first()
        if shop:
            shop_id = shop.id
        clist = clist.filter(Q(shop_id=shop_id) | Q(name__contains=kw))
        mywhere.append('keyword=' + kw)

    clist = clist.order_by("shop_id")
    list2, plist, pIndex, maxpages = get_page(clist, pIndex, 10)
    # 当前菜品分类信息封装对应的店铺信息
    for vo in list2:
        sob = Shop.objects.get(id=vo.shop_id)
        vo.shopname = sob.name
    context = {"categorylist": list2, 'plist': plist, 'pIndex': pIndex, 'maxpages': maxpages, 'mywhere': mywhere}
    return render(request, "myadmin/category/index.html", context)


def loadCategory(request, sid):
    clist = Category.objects.filter(status__lt=9, shop_id=sid).values("id", "name")
    # 返回QuerySet对象，使用list强转成对应的菜品分类列表信息
    return JsonResponse({'data': list(clist)})


def add(request):
    slist = Shop.objects.values("id", 'name')
    context = {"shoplist": slist}
    return render(request, "myadmin/category/add.html", context)


def insert(request):
    try:
        ob = Category()
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.status = 1
        ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "添加成功！"}
    except Exception:
        context = {'info': "添加失败！"}
    return render(request, "myadmin/info.html", context)


def delete(request, cid=0):
    try:
        if Product.objects.filter(status__lt=9).filter(category_id=cid):
            context = {'info': "请先删除菜品信息！"}
        else:
            ob = Category.objects.get(id=cid)
            ob.status = 9
            ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ob.save()
            context = {'info': "删除成功！"}
    except Exception:
        context = {'info': "删除失败！"}
    return render(request, "myadmin/info.html", context)


def edit(request, cid=0):
    try:
        ob = Category.objects.get(id=cid)
        context = {'category': ob}
        slist = Shop.objects.values("id", 'name')
        context["shoplist"] = slist
        return render(request, "myadmin/category/edit.html", context)
    except Exception:
        context = {'info': "没有找到要修改的信息！"}
        return render(request, "myadmin/info.html", context)


def update(request, cid):
    try:
        ob = Category.objects.get(id=cid)
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        ob.save()
        context = {'info': "修改成功！"}
    except Exception:
        context = {'info': "修改失败！"}
    return render(request, "myadmin/info.html", context)
